package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import src.util.Message;
import util.HibernateUtil;

public class NetPlayServlet extends HttpServlet{
	private PrintWriter				printWriter			= null;
	private HttpSession				httpSession			= null;
	private HttpServletRequest		req					= null;
	private HttpServletResponse		resp				= null;
	private Logger log = Logger.getLogger(NetPlayServlet.class);
	
	// ---------------------------------------------------------------------------------------------

	protected void doGet( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		doPost( req, resp);
	}

	// ---------------------------------------------------------------------------------------------

	protected void doPost( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		this.req = req;
		this.resp = resp;

		req.setCharacterEncoding( "UTF-8");
		
		String servletPath = req.getServletPath( );
		

		String[ ] methodNames = servletPath.split( "/");

		Method[ ] methods = getClass( ).getDeclaredMethods( );
		for( Method method : methods)
		{
			if( method.getName( ).equalsIgnoreCase( methodNames[methodNames.length - 1]))
			{
				try
				{
					Cookie[ ] cookies = req.getCookies( );
					if( cookies != null)
					{
						for( Cookie cookie : cookies)
						{

						}
					}
					httpSession = req.getSession( true);

					printWriter = resp.getWriter( );
					
					Object[ ] params = new Object[ ]{ req, resp};
					HibernateUtil.beginTransaction( );
					method.invoke(this ,null);
					HibernateUtil.commitOrRollback( );
//					printWriter.close( );
					HibernateUtil.closeSession( );
					return;
				}
				catch( Throwable ex)
				{
					HibernateUtil.commitOrRollback( );
					
					StackTraceElement[ ] tabSte = ex.getCause( ).getStackTrace( );
					for( StackTraceElement ste : tabSte)
					{
						if( ste.getMethodName( ).contains( "invoke"))
						{
							break;
						}
					}
					Message message = new Message( );
					send( message.toClient( 400, "Ko"));
//					printWriter.close( );
					HibernateUtil.closeSession( );
					return;
				}
			}
		}
		HibernateUtil.closeSession( );
		Message message = new Message( );
		send( message.toClient( 401, "Ko"));
//		printWriter.close( );
	}
	
	

	// ---------------------------------------------------------------------------------------------

	protected void send( String string)
	{
		try
		{
			printWriter.print( string);
		}
		catch(Exception e)
		{
			log.error(e);
		}
	}
	
	
	
	// ---------------------------------------------------------------------------------------------
	
	
	public HttpSession getHttpSession() {
		return httpSession;
	}
	public void setHttpSession(HttpSession httpSession) {
		this.httpSession = httpSession;
	}
	public HttpServletRequest getReq() {
		return req;
	}
	public void setReq(HttpServletRequest req) {
		this.req = req;
	}
	public HttpServletResponse getResp() {
		return resp;
	}
	public void setResp(HttpServletResponse resp) {
		this.resp = resp;
	}
	
	
	
}
